package com.jzo2o.market.controller.web;

import com.jzo2o.api.market.dto.request.CouponUseBackReqDTO;
import com.jzo2o.api.market.dto.request.CouponUseReqDTO;
import com.jzo2o.api.market.dto.response.AvailableCouponsResDTO;
import com.jzo2o.api.market.dto.response.CouponUseResDTO;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.market.model.dto.request.CouponOperationPageQueryReqDTO;
import com.jzo2o.market.model.dto.request.CouponRedeemReqDTO;
import com.jzo2o.market.model.dto.response.CouponUserInfoResDTO;
import com.jzo2o.market.service.ICouponUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

@RestController("webCouponUserController")
@RequestMapping("/web/coupon-user")
@Api(tags = "外部系统对接 - 优惠券相关接口")
public class CouponUserController {

    @Resource
    private ICouponUserService couponUserService;

    @GetMapping("/my")
    @ApiOperation("我的优惠券列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "lastId", value = "上一次查询最后一张优惠券id", required = false, dataTypeClass = Long.class),
            @ApiImplicitParam(name = "status", value = "优惠券状态，1:未使用，2:已使用，3:已过期", required = true, dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "userId", value = "用户id", required = true, dataTypeClass = Long.class)
    })
    public List<CouponUserInfoResDTO> queryMyCouponForPage(@RequestParam(value = "lastId", required = false) Long lastId,
                                                           @RequestParam(value = "status", required = true) Integer status,
                                                           @RequestParam(value = "userId", required = true) Long userId) {
        return couponUserService.queryForList(lastId, userId, status);
    }

    @PostMapping("/redeemCoupon")
    @ApiOperation("兑换优惠券")
    public void redeemCoupon(@RequestBody CouponRedeemReqDTO couponRedeemReqDTO) {
        couponUserService.redeemCoupon(couponRedeemReqDTO);
    }

    @GetMapping("/page")
    @ApiOperation("根据优惠券配置id查询")
    public PageResult<CouponUserInfoResDTO> queryForPage(CouponOperationPageQueryReqDTO couponOperationPageQueryReqDTO) {
        return couponUserService.queryForPageOfOperation(couponOperationPageQueryReqDTO);
    }

    @GetMapping("/getAvailable")
    @ApiOperation("获取可用优惠券列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "totalAmount", value = "总金额，单位分", required = true, dataTypeClass = BigDecimal.class),
            @ApiImplicitParam(name = "userId", value = "用户id", required = true, dataTypeClass = Long.class)
    })
    public List<AvailableCouponsResDTO> getAvailable(@RequestParam("totalAmount") BigDecimal totalAmount, @RequestParam("userId") Long userId) {
        return couponUserService.getAvailable(totalAmount, userId);
    }

    @PostMapping("/use")
    @ApiOperation("使用优惠券，并返回优惠金额")
    public CouponUseResDTO use(@RequestBody CouponUseReqDTO couponUseReqDTO) {
        return couponUserService.use(couponUseReqDTO);
    }

    @PostMapping("/useBack")
    @ApiOperation("优惠券退回接口")
    public void useBack(@RequestBody CouponUseBackReqDTO couponUseBackReqDTO) {
        couponUserService.useBack(couponUseBackReqDTO);
    }
}
